package DBMailDAO;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import mail.Mail;
import mail.User;
import mailDAO.MailDAO;

public class DBMailDAO implements MailDAO{
	private SessionFactory sessionFactory;
	public DBMailDAO(){
		Configuration config=new Configuration().configure();  
		sessionFactory=config.buildSessionFactory();
	}
	
	public Mail loadMailById(long id){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			Mail mail=null;
			mail = (Mail) sn.load(Mail.class,id);			
			return mail;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	
	public List<Mail> loadMailNotRead(User toUser){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.laodMailNotRead");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setBoolean("read", true);
			query.setEntity("toUser", toUser);			
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	public List<Mail> loadPageMailNotRead(User toUser,int recordStart, int pageSize){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.laodMailNotRead");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setBoolean("read", true);
			query.setEntity("toUser", toUser);
			query.setFirstResult((recordStart - 1) * pageSize);
			query.setMaxResults(pageSize);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	public List<Mail> loadMailRead(User toUser){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.laodMailRead");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setBoolean("read", true);
			query.setEntity("toUser", toUser);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	
	public List<Mail> loadPageMailRead(User toUser,int recordStart, int pageSize){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.laodMailRead");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setBoolean("read", true);
			query.setEntity("toUser", toUser);
			query.setFirstResult(recordStart);
			query.setMaxResults(pageSize);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	
	public List<Mail> loadMailInbox(User toUser){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.loadMailInbox");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setEntity("toUser", toUser);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
			
	}
	
	public List<Mail> loadPageMailInbox(User toUser,int recordStart, int pageSize){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.loadMailInbox");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setEntity("toUser", toUser);
			query.setFirstResult(recordStart);
			query.setMaxResults(pageSize);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	
	public List<Mail> loadMailOutbox(User fromUser){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.loadMailOutbox");
			query.setBoolean("fromDelete", true);
			query.setBoolean("fromDustbin", true);
			query.setEntity("fromUser", fromUser);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
			
	}
	
	public List<Mail> loadPageMailOutbox(User fromUser,int recordStart, int pageSize){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.loadMailOutbox");
			query.setBoolean("fromDelete", true);
			query.setBoolean("fromDustbin", true);
			query.setEntity("fromUser", fromUser);
			query.setFirstResult(recordStart);
			query.setMaxResults(pageSize);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
				
	}
	public List<Mail> loadMailDustbin(User toUser){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.loadMailDustbin");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setEntity("toUser", toUser);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	public List<Mail> loadPageMailDustbin(User toUser,int recordStart, int pageSize){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			Query query = sn.getNamedQuery("DBMailDAO.loadMailDustbin");
			query.setBoolean("toDelete", true);
			query.setBoolean("toDustbin", true);
			query.setEntity("toUser", toUser);
			query.setFirstResult(recordStart);
			query.setMaxResults(pageSize);
			List<Mail> mailList = query.list();
			return mailList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	public void setMailRead(Mail mail){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			mail.setRead(true);
			sn.saveOrUpdate(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}			
	}
	
	public void setReadTime(Mail mail, Date readTime){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			mail.setReceiveTime(readTime);
			sn.saveOrUpdate(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
	}
	
	public void fromUserSetMailDustbin(Mail mail){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			mail.setFromDustbin(true);
			sn.saveOrUpdate(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}			
	}
	public void toUserSetMailDustbin(Mail mail){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			mail.setToDustbin(true);
			sn.saveOrUpdate(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}			
	}
	public void toUserDeleteMail(Mail mail){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			mail.setToDelete(true);
			sn.saveOrUpdate(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}			
	}
	public void fromUserDeleteMail(Mail mail){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			mail.setFromDelete(true);
			sn.saveOrUpdate(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}			
	}
	public void saveMail(Mail mail){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{				
			sn.save(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}		
	}
	public void deleteMail(Mail mail){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{				
			sn.delete(mail);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}	
	}
}
